import 'package:kmxy_base/base/base_package.dart';
import 'package:kmxy_base/page/map_select/map_select_page_view.dart';
import 'package:kmxy_base/util/amap_location_util.dart';

import 'package:amap_flutter_base/amap_flutter_base.dart';

import 'package:amap_flutter_map/amap_flutter_map.dart';

abstract class MapSelectPageState extends BasePageState<MapSelectPage> {
  late AMapWidget map;
  late AMapController aMapController;

  Map addressData = {};

  String address = '';

  double lttd = 0;
  double lgtd = 0;

  bool isRestLocation = true;

  @override
  destroyData() {}

  late GridListViewController gridListViewController;

  onGridListViewCreated(GridListViewController controller) {
    gridListViewController = controller;
  }

  @override
  initData() {}

  initMap() {
    ///使用默认属性创建一个地图
    map = AMapWidget(
      apiKey: AMapApiKey(
          androidKey: widget.arguments?['amapAndroidKey'],
          iosKey: widget.arguments?['amapiOSKey']),
      onMapCreated: onMapCreated,
      onCameraMoveEnd: (CameraPosition argument) async {
        if (isRestLocation) {
          addressData = await AMapLocationUtil.getInstance
              .getAddressAllData(argument.target);
          setState(() {});
        }
        isRestLocation = true;
      },
    );
  }

  /// 地图创建完成的回调
  onMapCreated(AMapController controller) {
    aMapController = controller;
    AMapLocationUtil.getInstance.updateMapPosition(
        AMapLocationUtil.getInstance.currentLatLng, controller,
        zoom: 16);
  }

  itemClick(Map itemData) {
    for (Map data in addressData['pois']) {
      data['select'] = false;
    }
    itemData['select'] = true;
    address =
        '${addressData['addressComponent']['province']}${addressData['addressComponent']['city']}${addressData['addressComponent']['district']}${itemData['businessarea']}${itemData['name']}'
            .replaceAll('[', '')
            .replaceAll(']', '');
    isRestLocation = false;
    List latlng = itemData['location'].split(',');
    lttd = double.parse(latlng[1]);
    lgtd = double.parse(latlng[0]);
    AMapLocationUtil.getInstance
        .updateMapPosition(LatLng(lttd, lgtd), aMapController, zoom: 16);
    setState(() {});
  }

  sure() {
    finishPop(context, callBackData: {
      'address': address,
      "lttd": lttd,
      "lgtd": lgtd,
    });
  }

  location() {
    AMapLocationUtil.getInstance.updateMapPosition(
        AMapLocationUtil.getInstance.currentLatLng, aMapController,
        zoom: 16);
    setState(() {});
  }
}
